<template>
  <div class="about">
    <h1>{{ id ? "编辑" : "新建" }}广告位</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>

      <el-form-item label="广告">
        <el-col :md="24">
          <el-button @click="model.items.push({})"
            ><el-icon><Plus /></el-icon>添加广告</el-button
          ></el-col
        >
        <el-row type="flex" style="flex-wrap: wrap; width: 100%">
          <el-col
            :md="24"
            v-for="(item, i) in model.items"
            :key="i"
            style="margin-top: 20px"
          >
            <el-form-item label="跳转链接(URL)">
              <el-input v-model="item.url"></el-input>
            </el-form-item>
            <el-form-item label="图片" style="margin-top: 20px">
              <el-upload
                v-model="item.image"
                class="avatar-uploader"
                action="http://localhost:3000/admin/api/upload"
                :show-file-list="false"
                :headers="headers"
                :on-success="(res) => (item.image = res.url)"
                :before-upload="beforeUpload"
              >
                <img v-if="item.image" :src="item.image" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button
                size="small"
                type="danger"
                @click="model.items.splice(i, 1)"
                >删除</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { ref, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import { postNewCategory, getDetail, putDetail } from "@/api";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
export default {
  props: {
    id: {},
  },
  components: {
    Plus,
  },
  setup(props) {
    var model = ref({
      items: [],
    });
    const router = useRouter();
    async function save() {
      let res = props.id
        ? await putDetail("ads", props.id, model)
        : await postNewCategory("ads", model);
      console.log(res);
      router.push("/ads/list");
      ElMessage({
        message: "保存成功",
        type: "success",
      });
    }
    async function fetch() {
      const res = await getDetail("ads", props.id);
      // 因为服务端没有items 会覆盖 {}, model.value, res.data 将三个源对象的数据综合赋予model
      model.value = Object.assign({}, model.value, res.data);
    }
    const headers = {
      Authorization: `Bearer ${localStorage.token}`,
    };
    onBeforeMount(() => {
      // 组件初次在页面渲染之前（创建阶段）唯一一次
      props.id && fetch();
    });
    return { model, save, headers };
  },
};
</script>

<style></style>
